package com.service;

import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IRetCusPrdInDao;
import com.dao.IRetTmCusPrdInDao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_cus_prd_in;
import com.model.Ret_tm_cus_prd_in;
import com.tx.Xpinqcusprd.XpinqcusprdI;
import com.tx.Xpinqcusprd.XpinqcusprdO;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpinqcusprdService")
public class XpinqcusprdService extends ICIMBaseService{

	@Autowired
	private MqOpe mqOpe;
	
	@Autowired
	private IRetCusPrdInDao retCusPrdInDao;
	
	@Autowired
	XpinqcusprdI inTrx;

	@Autowired
	XpinqcusprdO outTrx;
	
	@Autowired
	private IRetTmCusPrdInDao retTmCusPrdInDao;

	private Logger logger = Logger.getLogger(XpinqcusprdService.class);

	public String subMainProc(String strInMsg) {
		String reply = "";
		try {
			inTrx = (XpinqcusprdI) mqOpe.inStr2Obj(strInMsg, inTrx);
			outTrx.setTrx_id("XPINQCUSPRD");
			outTrx.setType_id("O");
			logger.info("InTrx is " + strInMsg);

			String actionFlg = inTrx.getAction_flg();
			if (actionFlg.equals("Q")) {
				 getCusPrdProc();
			}else if(actionFlg.equals("X")){
				getXmtmCusPrdProd();
			}else if(actionFlg.equals("B")){
				getMtrlBoxInfo();
			}
			if ((RETURN_CODE_OK).equals(outTrx.getRtn_code())) {
				retCusPrdInDao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			logger.error(Util.stackTraceToString(ex));
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			reply = mqOpe.obj2Str(outTrx);
		}
		return reply;

	}

	public void  getCusPrdProc() {
		List<Ret_cus_prd_in> retCusPrdInList = new ArrayList<Ret_cus_prd_in>();
		String sql="FROM Ret_cus_prd_in WHERE (act_flg IS NULL OR act_flg<>'Y')";
		retCusPrdInList = retCusPrdInDao.find(sql);
		outTrx.setOary_prd(retCusPrdInList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	public void getXmtmCusPrdProd() {
		String sql = "FROM Ret_tm_cus_prd_in where (act_flg is null or act_flg <> ? )";
		List<Ret_tm_cus_prd_in> tmCusPrdOaryList = retTmCusPrdInDao.list(sql, "Y");
		outTrx.setOary_tm_prd(tmCusPrdOaryList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	public void getMtrlBoxInfo() {
		String sql = "FROM Ret_tm_cus_prd_in where boxname = ? and (act_flg is null or act_flg <> ? )";
		List<Ret_tm_cus_prd_in> tmCusPrdOaryList = retTmCusPrdInDao.list(sql, inTrx.getBox_id(),"Y");
		outTrx.setOary_tm_prd(tmCusPrdOaryList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

}
